<?php
/**
 * Date: 10/22/12
 * Time: 12:00 AM
 * User: Amitav Roy
 *
 * This is the global helper file which will be used for common function.
 */

/**
 * This function will be used when ever I want to link any page.
 * This will add the index.php automatically with the base_url
 * which is required in almost all links.
 */
if ( ! function_exists('base_path'))
{
	function base_path() {
		$CI =& get_instance();
		$path = base_url();
		print $path;
	}
}

/**
 * This function will check the authentication of the current user.
 * If the user is not authenticated, he will be redirected to login
 * page automatically.
 */
if ( ! function_exists('auth_user'))
{
	function auth_user() {
		$CI =& get_instance();
		$auth = $CI->session->userdata('auth');
		if ($auth != 1) {
			redirect('user/login');
		}
	}
}

/**
 * This is the develop function to print an array,
 * this is the most important function during development.
 */
if ( ! function_exists('dsm'))
{
	function dsm($var) {
		print '<pre>';
		print_r ($var);
		print '</pre>';
	}
}

/**
 * This function is used to set message and that will be displayed as an alert
 * of different kind to the end user.
 * Success, alert and wrong messages are shown by this function.
 */
if ( ! function_exists('set_message'))
{
    function set_message($message, $type = 'success') {
        $CI =& get_instance();
        $output = '';
        $output .= '<div class="alert alert-'. $type .'">';
        $output .=  $message;
        $output .= '</div>';

        $text = $CI->session->set_flashdata('alert_message', $output);
    }
}

if ( ! function_exists('get_message'))
{
    function get_message() {
        $CI =& get_instance();
        $message = $CI->session->flashdata('alert_message');
        if (isset($message) && $message != '')
            print $message;
        else
            print '';
    }
}

if (!function_exists('is_ajax_req'))
{
  function is_ajax_req() {
    if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest")
      return true;
    else
      return redirect(base_url());
  }
}